package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.entity.mes.dto.WorkShopAddDto;
import com.example.entity.mes.dto.WorkShopDelDto;
import com.example.entity.mes.dto.WorkShopListDto;
import com.example.entity.mysql.Workshop;
import com.example.mapper.WorkshopMapper;
import com.example.service.WorkshopService;
import com.example.untils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 车间表 前端控制器
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-04-16
 */
@RestController
@RequestMapping("/api/workshop")
@Api(tags = "车间表")
public class WorkshopController {

    @Autowired
    private WorkshopMapper mapper;

    @Autowired
    private WorkshopService service;

    @ApiOperation("车间添加")
    @PostMapping("workShopAdd")
    public Result workShopAdd(@Valid @RequestBody WorkShopAddDto dto) {
        Workshop workshop = new Workshop();
        workshop.setWorkshopCode(dto.getWorkshopCode());
        workshop.setEnableFlag(dto.getIsUse());
        workshop.setArea(dto.getArea());
        workshop.setRemark(dto.getRemark());
        workshop.setCharge(dto.getPrincipal());
        workshop.setWorkshopName(dto.getWorkshopName());
        boolean b = service.save(workshop);
        if(b) return Result.success("添加成功");
        return Result.error("添加错误");
    }

    @ApiOperation("车间列表")
    @PostMapping("workShopList")
    public Result<PageInfo<List<Workshop>>> workShopList(@RequestParam(defaultValue = "0") Integer current,
                                               @RequestParam(defaultValue = "10") Integer size,
                                               @RequestBody WorkShopListDto dto) {
        PageHelper.startPage(current, size);
        LambdaQueryWrapper<Workshop> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(dto.getWorkshopCode())) {
            wrapper.like(Workshop::getWorkshopCode, dto.getWorkshopCode());
        }
        if(StringUtils.isNotEmpty(dto.getWorkshopName())) {
            wrapper.like(Workshop::getWorkshopName, dto.getWorkshopName());
        }
        List<Workshop> workshops = mapper.selectList(wrapper);
        PageInfo<List<Workshop>> pageInfo = new PageInfo(workshops);
        return Result.success(pageInfo);
    }

    @ApiOperation("删除")
    @PostMapping("workShopDel")
    public Result workShopDel(@RequestBody WorkShopDelDto dto) {
        if(StringUtils.isEmpty(dto.getWorkshopId())) {
            return Result.error("车间ID为空");
        }
        int byId = mapper.deleteById(dto.getWorkshopId());
        if(byId > 0) {
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败, 没有该编号");
        }
    }

    @ApiOperation("修改")
    @PostMapping("workShopModify")
    public Result workShopModify(@RequestBody Workshop workshop) {
        if(workshop.getWorkshopId() == null) {
            return Result.error("车间ID为空");
        }
        boolean b = service.updateById(workshop);
        if(b) return Result.success("更新成功");
        return Result.error("更新失败");
    }

}
